package com.rao.quartz.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.stream.IntStream;

/**
 * @Author: raoguang
 * @Date: 2022/05/27/9:10
 * @Description:
 */
@Service
public class asynServiceImpl implements asynService {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Async
    @Override
    public String testAsyns() {

        Object building = new Object();
        synchronized (building) {
            try {
                for (int i = 1; i <= 100; i++) {
                    System.out.println(Thread.currentThread().getName() + "----------异步：>" + i);
                    //logger.info(Thread.currentThread().getName()+"----------异步：>"+i);
                    building.wait(200);
                }
                return "执行异步任务完毕";
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return Thread.currentThread().getName() + "执行完毕";
    }

    //@Async
    @Override
    public void sendSms() {
        System.out.println(Thread.currentThread().getName() + "--sendSms--2");
        IntStream.range(0, 5).forEach(d -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(Thread.currentThread().getName() + "--sendSms--3");
    }

    @Async("NormalExecutor")
    @Override
    public void task1() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread.sleep(1000);
        long end = System.currentTimeMillis();
        System.out.println("线程：" + Thread.currentThread().getName() + "任务1总耗时：" + (end - begin));
    }

    @Async("NormalExecutor")
    @Override
    public void task2() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread.sleep(2000);
        long end = System.currentTimeMillis();
        System.out.println("线程：" + Thread.currentThread().getName() + "任务2总耗时：" + (end - begin));
    }

    @Async("NormalExecutor")
    @Override
    public void task3() throws InterruptedException {
        long begin = System.currentTimeMillis();
        Thread.sleep(3000);
        long end = System.currentTimeMillis();
        System.out.println("线程：" + Thread.currentThread().getName() + "任务3总耗时：" + (end - begin));
    }

}
